layui.use(['jquery','form','layer','laydate','upload'],function () {
    var form = layui.form;
    var $ = layui.$;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var upload = layui.upload;
    getDepts();
    $('#refresh').click(function () {
        location.reload()
    });
    //添加按钮
    $('#addUser').click(function () {
        $('#registerDate').val(setTime());
        $.ajax({
            url: '/login/addUser',
            data: form.val('addUserForm'),
            type: 'post',
            dataType: 'json',
            success: function (data) {
                if(data.data.userId != null){
                    $('input,textarea').val('');
                    $('img').attr('src','');
                    parent.$('#quickEntry').val('后台用户列表');
                    parent.quickEntry();
                }else{
                    layer.msg('添加失败');
                }
            }
        });
    });
    //返回按钮
    $('#goBack').click(function () {
        var iframe = parent.$('.iframe');
        $.each(iframe,function (i,item) {
            if('添加后台人员' == $(item).attr('_href')){
                parent.$('#beforeHref').val($(item).attr('_href'));
                parent.beforeHref();
                return;
            }
        });
    });
    $('#registerIp').val(returnCitySN.cip);
    //修改头像按钮
    upload.render({
        elem: '#acatarUrl',
        url: '/login/uploadUserAcatar',
        field: 'file',
        exts: 'png|jpg|gif',
        done: function(res, index, upload){
            if(res.code == 0){
                layer.msg('上传成功');
                $('#acatarUrl').attr('src',res.data.data.src);
                $('#acatarUrlVal').val(res.data.data.src);
            }
        }
    });
    //出生日期渲染
    laydate.render({
        elem: '#birthDate',
        type: 'datetime'
    });
    //查询所有部门
    function getDepts() {
        $.ajax({
            url: '/SyDept/getDepts',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                var option = data.data;
                var deptId = $('#dept').val();
                for (var i = 0; i < option.length; i++) {
                    if(option[i].id != 0){
                        $('#deptId').append('<option value="' + option[i].id + '">' + option[i].deptName + '</option>');
                    }
                }
                $('#deptId').val(deptId);
                form.render();
            }
        });
    }
    //获取 年月日 时分秒 星期
    function setTime(){
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var today = date.getDate();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();
        //当小时小于10时进行补零
        var currentTime = zeroFill(year) + '-' + zeroFill(month) + '-' + zeroFill(today) +  ' ' + zeroFill(hour) + ':' + zeroFill(minute) + ':' + zeroFill(second);
        return currentTime;
    }
    //补零
    function zeroFill(time){
        if(time < 10){
            time = '0' + time;
        }
        return time;
    }
});